package bot;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import aswerGenerator.GetAnswerData;
import aswerGenerator.PeriodNotFoundException;
import aswerGenerator.ProgramNotFoundException;

import lexicalUnit.Environ;
import lexicalUnit.LexicalException;

import chalmersbot.PrettyPrinter;
import chalmersbot.Yylex;
import chalmersbot.parser;
import dbUnit.MysqlConnect;

public class Main {
	
	private static File auxFile = null;
	private static File testFile = null;
	
	private static void getTestFile (){
		testFile = new File ("testFile");
		try{
			if (!testFile.exists())
				throw new FileNotFoundException();
		}catch(IOException e){
			System.err.println("IO exception: " + e.getMessage());
			System.exit(1);
		}
	}
	
	private static void getAuxFile () {
		auxFile = new File ("auxFile");
		try{
			if (!auxFile.exists())
				auxFile.createNewFile();	
		}catch(IOException e){
			System.err.println("IO exception: " + e.getMessage());
			System.exit(1);
		}
	}
	
	private static void writeAuxFile (String s){
		try{
			FileWriter fstream = new FileWriter(auxFile);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(s);
			out.close();
		}catch(IOException e){
			System.err.println("IO exception: " + e.getMessage());
			System.exit(1);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {	
		String input = "";
		InputStreamReader inStream = new InputStreamReader(System.in);
		BufferedReader in = new BufferedReader(inStream);
		Yylex l = null;
		parser p = null;
		boolean isTest = false;
		Environ env = new Environ();
		
		getTestFile ();			
		getAuxFile();		
		System.out.println("System: Welcome to ChalmersBot. Please ask me a question. ");		
		try{
			//InputStreamReader testStream = new InputStreamReader(new FileInputStream(testFile));
			//BufferedReader reader = new BufferedReader(testStream);
			boolean newInfo = false;
			/*if (isTest){
				input = reader.readLine();
				System.out.println(input);
			}
				else*/
			System.out.print("User: ");
			input = in.readLine();
			while (!(input.equals("bye"))){
				/*if (input.startsWith("--") | input.compareTo("")==0){
					input = reader.readLine();
					System.out.println("User: " + input);
					continue;
				}*/
				writeAuxFile(input);
				
			    l = new Yylex(new FileReader(auxFile));
			    p = new parser(l);
			    try
			    {
			      chalmersbot.Absyn.Sentence parse_tree = p.pSentence();
			      /*System.out.println();
			      System.out.println(PrettyPrinter.show(parse_tree));
			      System.out.println();*/
			      if (newInfo)
			    	  env = new Environ();
			      else
			    	  newInfo = true;
			      System.out.println("System: " +  env.getAnswer(parse_tree));
			    }catch(LexicalException e){
			    	System.out.println(e.getMessage());
			    }catch(PeriodNotFoundException e){
			    	newInfo = false;
			    	System.out.println("System: in which period ?");
			    }catch(ProgramNotFoundException e){
			    	newInfo = false;
			    	System.out.println("System: in which program ?");
			    }catch(Throwable e)
			    {
			      //System.err.println("At line " + String.valueOf(l.line_num()) + ", near \"" + l.buff() + "\" :");
			      //System.err.println("     " + e.getMessage());
			      System.out.println("I cannot understand you, can you repeat please?\n");
			    }
			    /*if (isTest){
					input = reader.readLine();
					System.out.println("User: " + input);
				}else*/
			    System.out.print("User: ");
				input = in.readLine();
			}
		}catch (IOException e){
			//System.err.println("System internal error: " + e.getMessage());
		}
	}

}
